package 集合泛型;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class 排序 {
    public static void main(String[] args) {
        List<User> list =new ArrayList<>(List.of(new User(11,"张三",18),new User(12,"李四",19)));
        System.out.println(list);
        //乱序
        Collections.shuffle(list);
        System.out.println(list);
        //降序  根据User的年龄
        Collections.sort(list,(a,b)->b.getAge()-a.getAge());
        System.out.println(list);
        //升序  根据User的id
        Collections.sort(list,(a,b)->a.getId()-b.getAge());
        System.out.println(list);
        Collections.sort(list,(a,b)->a.getName().compareTo(b.getName()));
        System.out.println(list);
    }



    public static void main1(String[] args) {
        List<Integer> list=new ArrayList<>(List.of(10,5,20,6,3,100,200,9));
        System.out.println(list);
        //乱序
        Collections.shuffle(list);
        System.out.println(list);
        Collections.shuffle(list);
        System.out.println(list);
        //升序
        Collections.sort(list);
        System.out.println(list);
        //降序
        Collections.sort(list,(a,b)->b-a);
        System.out.println(list);
        //字符串List<String> 排序
        List<String> list1=new ArrayList<>(List.of("javascript","c","go","ruby","java","php","python"));
         //乱序
        System.out.println(list1);
        Collections.shuffle(list1);
        System.out.println(list1 );
        //升序
        Collections.sort(list1);
        System.out.println(list1);
        //降序
        Collections.sort(list1, Comparator.reverseOrder());
        System.out.println(list1);
        //按照字符串的长度排序
        Collections.sort(list1,(x,y)->y.length()-x.length() );
        System.out.println(list1);

    }
}
